﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CodePlex.Resourceful.Amazon.S3;

namespace CodePlex.Resourceful.Examples35
{
    public static class S3Example
    {

        public static void Run()
        {

       
            string[] aws = Environment.GetEnvironmentVariable("AWS").Split(':');

            string accessKeyID = aws[0];
            string secretAccessKey = aws[1];

            S3Connection cn = new S3Connection(accessKeyID, secretAccessKey, false, RequestStyle.Path);

            // gather bucket information
            var bucketinfo = (from b in cn.ListBuckets()
                              let keys = cn.ListBucket(b.Name)
                              select new { Name = b.Name, KeyCount = keys.Count, KeySize = keys.Sum(k => k.Size) }).ToList();

            // all buckets ordered by key count (desc)
            foreach (var bi in bucketinfo.OrderByDescending(bi => bi.KeyCount))
            {
                Console.WriteLine("{1,15} {0}", bi.Name, bi.KeyCount);
            }

            // all buckets ordered by key size (desc)
            foreach (var bi in bucketinfo.OrderByDescending(bi => bi.KeySize))
            {
                Console.WriteLine("{1,15} {0}", bi.Name, Utils.ComputeSizeString(bi.KeySize));
            }


            //largest 10 keys in 5 largest buckets
            foreach (var bi in bucketinfo.OrderByDescending(bi => bi.KeySize).Take(5))
            {
                Console.WriteLine("bucket:{0}", bi.Name);
                foreach (var k in cn.ListBucket(bi.Name).OrderByDescending(k => k.Size).Take(10))
                {
                    Console.WriteLine("{1,15} {0}", k.Name, Utils.ComputeSizeString(k.Size));
                }
            }

          
        }
    }
}
